#!/bin/bash

SOURCE=$0
while [ -h $SOURCE ]; do
	DIR=$(cd `dirname $SOURCE`; pwd)
	SOURCE=`readlink $SOURCE`
	[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done

basePath=$(cd `dirname $SOURCE`; pwd)

. $basePath/glassMonitor.func

MsgLog info "${thisName} Start..."
MsgLog info "${thisName} Path: ${basePath}"

MainInit

failCount=0

while true
do
	#echo mainLoop...
	
	if [ `rmc list | grep Glassfish | grep stopped | wc -l` -gt 0 -o  `rmc list | grep Glassfish | grep degraded | wc -l` -gt 0 ]; then
		MsgLog error "glassfish status error!"
		rmc list >> $logFile
		
		failCount=$[$failCount+1]
		if [ $failCount -gt 3 ]; then
			MsgLog warn "failCount > 3, begin stop glassfish"
			rmc stop glassfish
		fi
		MsgLog info "begin start glassfish"
		rmc start
	else
		if [ $failCount -gt 0 ]; then
			MsgLog info "glassfish status OK."
		fi
		failCount=0
	fi
	
	sleep 300
done
